package com.example.apijson;

import apijson.JSONParser;
import apijson.Log;
import apijson.framework.APIJSONApplication;
import apijson.framework.APIJSONCreator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.example.apijson.config.MyFunctionParser;
import com.example.apijson.config.MyParser;
import com.example.apijson.config.MySQLConfig;
import com.example.apijson.config.MySQLExecutor;
import com.example.apijson.config.MyVerifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@SpringBootApplication
public class MyApiJsonApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyApiJsonApplication.class, args);

        Log.DEBUG = true;
        APIJSONApplication.init(false);  // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOriginPatterns("*")
                        .allowedMethods("*")
                        .allowCredentials(true)
                        .maxAge(3600);
            }
        };
    }

    static {
        // 使用 fastjson
        apijson.JSON.JSON_OBJECT_CLASS = JSONObject.class;
        apijson.JSON.JSON_ARRAY_CLASS = JSONArray.class;

        final Feature[] DEFAULT_FASTJSON_FEATURES = {Feature.OrderedField, Feature.UseBigDecimal};
        apijson.JSON.DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() {

            @Override
            public JSONObject createJSONObject() {
                return new JSONObject(true);
            }

            @Override
            public JSONArray createJSONArray() {
                return new JSONArray();
            }

            @Override
            public String toJSONString(Object obj, boolean format) {
                return obj == null || obj instanceof String ? (String) obj : JSON.toJSONString(obj);
            }

            @Override
            public Object parseJSON(Object json) {
                return JSON.parse(toJSONString(json), DEFAULT_FASTJSON_FEATURES);
            }

            @Override
            public JSONObject parseObject(Object json) {
                return JSON.parseObject(toJSONString(json), DEFAULT_FASTJSON_FEATURES);
            }

            @Override
            public <T> T parseObject(Object json, Class<T> clazz) {
                return JSON.parseObject(toJSONString(json), clazz, DEFAULT_FASTJSON_FEATURES);
            }

            @Override
            public JSONArray parseArray(Object json) {
                return JSON.parseArray(toJSONString(json));
            }

            @Override
            public <T> List<T> parseArray(Object json, Class<T> clazz) {
                return JSON.parseArray(toJSONString(json), clazz);
            }

        };

        // 使用本项目的自定义处理类
        APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long, JSONObject, JSONArray>() {

            @Override
            public MyParser createParser() {
                return new MyParser();
            }

            @Override
            public MyFunctionParser createFunctionParser() {
                return new MyFunctionParser();
            }

            @Override
            public MyVerifier createVerifier() {
                return new MyVerifier();
            }

            @Override
            public MySQLConfig createSQLConfig() {
                return new MySQLConfig();
            }

            @Override
            public MySQLExecutor createSQLExecutor() {
                return new MySQLExecutor();
            }

        };


    }

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(8081);
    }

}